#include "StdAfx.h"
#include "MidiParser.h"


int output_note=60;
int pre_note=60;

using namespace jdksmidi;
MidiParser::MidiParser(char * midi_path)
{
	rs=new MIDIFileReadStreamFile( midi_path );
	tracks=new MIDIMultiTrack( 64 );
	track_loader=new MIDIFileReadMultiTrack (tracks );
	reader=new MIDIFileRead  ( rs, track_loader );
	gui=new MIDISequencerGUIEventNotifierText( stdout );
	seq=new MIDISequencer ( tracks, gui );
	driver=new MIDIDriverDump  ( 128, stdout );
	mgr=new MIDIManager( driver, gui );
	reader->Parse();
	seq->GoToZero();
	mgr->SetSeq ( seq );
	DumpTrackNames ( seq );

	double pretend_clock_time = 0.0;
	seq->GoToTime ( ( unsigned long ) pretend_clock_time );
	mgr->SeqPlay();
}

MidiParser::~MidiParser(void)
{
	mgr->SeqStop();
	mgr->GetDriver()->AllNotesOff();
}





void MidiParser::DumpTrackNames ( MIDISequencer *seq )
{
	fprintf ( stdout, "TEMPO = %f\n",
		seq->GetTrackState ( 0 )->tempobpm
		);

	for ( int i = 0; i < seq->GetNumTracks(); ++i )
	{
		fprintf ( stdout, "TRK #%2d : NAME = '%s'\n",
			i,
			seq->GetTrackState ( i )->track_name
			);
	}
}


void MidiParser::PlayDumpManager ( MIDIManager *mgr )
{
	
	// simulate a clock going forward with 10ms resolution for 1 minute

	//for ( pretend_clock_time = 0.0; pretend_clock_time < 60.0 * 1000.0; pretend_clock_time += 100 )
	//{
	//	mgr->GetDriver()->TimeTick ( ( unsigned long ) pretend_clock_time );
	//}


}


void MidiParser::Run(int msec){
		mgr->GetDriver()->TimeTick ( ( unsigned long ) msec );
}

